/*********************************
 * jsmud: JavaScript MUD Kernel  *
 *          Mojodo, Inc.         *
 *        Sean Middleditch       *
 * Copyright (C) 2005,2007,2008  *
 *      All rights reserved.     *
 *   See COPYING for licensing.  *
 *********************************/

/* ==== SYSTEM INCLUDES ==== */

#include <string.h>
#include <errno.h>
#include <limits.h>
#include <arpa/telnet.h>
#include <sys/stat.h>

/* ==== LIBRARY INCLUDES ==== */

#include <sqlite3.h>
#include <zlib.h>

#define XP_UNIX 1
#define MOZILLA_1_8_BRANCH 1
#include <jsapi.h>
#undef XP_UNIX
#undef MOZILLA_1_8_BRANCH

/* ==== JSMUD HEADER ==== */

#include "jsmud.h"

/* ==== DECLARATIONS ==== */

JSAPI(js_timer_register);
JSAPI(js_timer_setscale);

/* ==== SCRIPT BINDINGS - Timer ==== */

static JSFunctionSpec js_timer_functions[] = {
	{"register", js_timer_register, 3, 0, 0},
	{"setScale", js_timer_setscale, 1, 0, 0},
	{NULL, 0, 0, 0, 0}
};

JSAPI(js_timer_register)
{
	double time;
	JSBool gametime;

	if (JS_ValueToNumber(cx, argv[0], &time) == JS_FALSE)
		return JS_FALSE;

	if (JS_ValueToFunction(cx, argv[1]) == NULL)
		return JS_FALSE;

	if (JS_ValueToBoolean(cx, argv[2], &gametime) == JS_FALSE)
		return JS_FALSE;

	if (timer_new(time, gametime, &argv[1]) == NULL)
		return JS_FALSE;

	return JS_TRUE;
}

JSAPI(js_timer_setscale)
{
	double scale;

	if (JS_ValueToNumber(cx, argv[0], &scale) == JS_FALSE)
		return JS_FALSE;

	if (scale < 0.0)
		return JS_FALSE;

	timer_game_scale = scale;

	return JS_TRUE;
}

/* ==== INITIALIZE BINDINGS ==== */

int init_timerlib (void)
{
	JSObject* ns_timer;

	/* create "namespace" object */
	if ((ns_timer = JS_DefineObject(js_context, js_global, "Timer", NULL, NULL, JSPROP_ENUMERATE|JSPROP_PERMANENT)) == NULL) {
		log_error("JS_DefineObject() failed");
		return 0;
	}

	/* create all our custom functions */
	if (!JS_DefineFunctions(js_context, ns_timer, js_timer_functions)) {
		log_error("JS_DefineFunctions() failed");
		return 0;
	}

	return 1;
}

/* vim: set tabstop=2 shiftwidth=2 ai: */
